// pages/exam/index/index.js
let app = getApp()
const { api } = require('../../../api/base.js');
Page({
  data: {
    paperType: 0,
    spinShow: false,
    loadMoreLoad: false,
    loadMoreTip: '暂无数据',
    queryParam: {
      paperType: 1,
      pageIndex: 1,
      pageSize: app.globalData.pageSize
    },
    tableData: [],
    total: 1
  },

  onLoad: function(options) {
    // this.setData({
    //   spinShow: true
    // });
    // this.search(true)
    this.getExerciseTypeList();
  },
  tabChange({
    detail
  }) {
    this.setData({
      spinShow: true
    });
    let size = app.globalData.pageSize;
    const index = detail.key;
    const info = this.data.paperCategory[index];
    this.setData({
      paperType: parseInt(info.id),
      sIndex: index,
      queryParam: {
        paperType: parseInt(info.id),
        pageIndex: 1,
        pageSize: app.globalData.pageSize
      }
    });
    this.search(true)
    // console.log(detail);
   
    // this.getPaperList(info.id);
  },
  onPullDownRefresh() {
    this.setData({
      spinShow: true
    });
    if (!this.loading) {
      this.setData({
        ['queryParam.pageIndex']: 1
      });
      this.search(true)
    }
  },
  onReachBottom() {
    if (!this.loading && this.data.queryParam.pageIndex < this.data.total) {
      this.setData({
        loadMoreLoad: true,
        loadMoreTip: '正在加载'
      });
      this.setData({
        ['queryParam.pageIndex']: this.data.queryParam.pageIndex + 1
      });
      this.search(false)
    }
  },
  search: function(override) {
    let _this = this
    api('/api/wx/student/list', 'POST', this.data.queryParam).then(res => {
      _this.setData({
        spinShow: false
      });
      wx.stopPullDownRefresh()
      if (res.code === 20000) {
        const re = res.data
        _this.setData({
          ['queryParam.pageIndex']: re.current,
          tableData: override ? re.records : this.data.tableData.concat(re.list),
          total: re.total
        });
        if (re.current > re.pages) {
          this.setData({
            loadMoreLoad: false,
            loadMoreTip: '暂无数据'
          });
        }else{
          this.setData({
            loadMoreLoad: false,
            loadMoreTip: ''
          });
      }
      }
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })
  },
  getPaperList(id) {
    const info = this.data.queryParam;
    info.paperType = id;
    api('/api/wx/student/list', 'POST', info).then(resp => {
      if (resp.result) {
        this.setData({
          tableData: resp.data.records,
          total: resp.data.total
        });
      } else {
        wx.showToast({
          title: '网络错误',
        })
      }
    }).catch(err => {
      throw new Error(err);
    })
  },
  getExerciseTypeList(){
    api('/exam/paperCategory/listAll', 'GET', {}).then(resp => {
      if (resp.result) {
        this.setData({
          paperCategory: resp.data
        })
      } else {
        wx.showToast({
          title: '网络错误',
        })
      }
    }).catch(err => {
      throw new Error(err);
    })
  }
})